package com.anjie.powerproject.repository;

import com.anjie.powerproject.entity.Relationship;
import com.anjie.powerproject.pojo.CustomRelationship;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface RelationshipRepository extends JpaRepository<Relationship,Integer> {

    @Query(nativeQuery = true,value = "select * from relationship where device_from = ?1")
    List<Relationship> findRelationshipByFrom(Integer deviceFrom);

    @Query(value = "select new com.anjie.powerproject.pojo.CustomRelationship(r.id,r.deviceTo,d.deviceName,r.lossRate,r.typeId) from Relationship r left  join Device d on r.deviceTo = d.deviceId where r.deviceFrom=:deviceId")
    List<CustomRelationship> findCustomRelationship(@Param("deviceId") Integer deviceId);

    Relationship findRelationshipById(Integer id);

    List<Relationship> findRelationshipsByDeviceFromEquals(Integer deviceFrom);

    List<Relationship> findRelationshipsByDeviceToEquals(Integer deviceTo);

    List<Relationship> findRelationshipsByGraphIdEquals(Integer graphId);

    List<Relationship> findRelationshipsByDeviceFromAndAndDeviceToAndGraphId(Integer deviceFrom,Integer deviceTo,Integer graphId);

    void deleteRelationshipsByGraphId(Integer graphId);

    List<Relationship> findRelationshipsByTypeId(Integer typeId);

    List<Relationship> findRelationshipsByStgyCodeEquals(String stgyCode);

    List<Relationship> findRelationshipsByDeviceFromAndGraphId(Integer deviceFrom,Integer graphId);

    List<Relationship> findRelationshipsByDeviceToAndGraphId(Integer deviceTo,Integer graphId);

    @Query(nativeQuery = true,value = "select distinct device_to from relationship where graph_id = ?1")
    List<Integer> findDeviceTosByGraphIdDistinct(Integer graphId);

}
